<?php

App::uses('AppController', 'Controller');

class AdminController extends AppController {

    /**
     * 
     */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->loadModel('Customers');
        $this->loadModel('Admins');
        $this->menu_active = 'admin';
    }

    public function index() {
        $condition = array('IsAdmin' => 1);
        if (isset($_GET['submit'])) {
            if (!empty($_GET['Other'])) {
                $condition['OR'] = array(
                    'Customers.CustId like' => '%' . $_GET['Other'] . '%',
                    'Customers.CustName like' => '%' . $_GET['Other'] . '%',
                    'Customers.CustFurigana like' => '%' . $_GET['Other'] . '%',
                    'Customers.CustAddress like' => '%' . $_GET['Other'] . '%',
                    'Customers.CustPhone like' => '%' . $_GET['Other'] . '%',
                    'Customers.Email like' => '%' . $_GET['Other'] . '%',
                );
            }else{
                $condition['AND'] = array(
                    'Customers.CustId like' => '%' . $_GET['Id'] . '%',
                    'Customers.CustName like' => '%' . $_GET['Name'] . '%',
                    'Customers.Email like' => '%' . $_GET['Email'] . '%',
                );
            }
        }
       

        $data = $this->Customers->getData($condition);
        $this->set('data', $data);
    }

    public function add() {
        if ($this->request->data) {
            $this->request->data['Customers']['IsAdmin'] = 1;
            $this->request->data['Customers']['CrtDt'] = date('Y-m-d H:i:s');
            $result = $this->Customers->saveData($this->request->data);

            if ($result) {
                $this->Session->setFlash(__('Your Admin has been added.'));
                $this->redirect("/admin/index");
                exit;
            }
        }
    }

    public function edit($id) {
        if (empty($id)) {
            throw new NotFoundException(__('Not exsit id'));
        }
        $is_edit = 1;
        $data = $this->Customers->getData(null, $id);
        if ($data) {
            if ($this->request->data) {
                $result = $this->Customers->saveData($this->request->data, $id);
                if ($result) {
                    $this->Session->setFlash(__('Your Admin has been updated.'));
                    $this->redirect("/admin/index");
                    exit;
                }
            }
            $data['Customers']['Pass'] = '';
            if (!$this->request->data) {
                $this->request->data = $data;
            }
            $this->set('is_edit', $is_edit);
            $this->render('add');
        } else {
            $this->redirect("/admin/index");
            exit;
        }
    }

    public function delete($adminId) {
        $this->autoRender = FALSE;
        if (empty($adminId))
            throw new NotFoundException(__('Not exsit AdminID'));
        if ($this->Session->read('Auth.User.CustId') == $adminId)
            throw new NotFoundException(__('Can\'t not delete because this is Your Acount'));
        if ($this->Customers->delete($adminId)) {
            $this->Session->setFlash(__('The Admin with id: %s has been deleted.', h($adminId)));
            return $this->redirect(array('action' => 'index'));
        }
    }

}
